define(function(require) {

    'use strict';

    var Base = require('components/pop/base'),
        template = require('text!components/pop/templates/reply.html'),
        Editor = require('components/editor_v2/com'),
        utils = require('core/utils');

    return Base.extend({

        template: _.template(template),
        className: function() {
            return this.options.className;
        },

        events: {
            'click .js-close': 'hide',
            'click .js-btn-confirm': 'triggerCallback'
        },

        initialize: function(options) {
            var view = this;

            Base.prototype.initialize.call(view, options);
            view.data = options.data;
        },

        render: function() {
            var view = this;

            var data = view.data
            , minus = data.minus;
            view.$el.html(view.template(data));

            view.editorEle = view.$el.find('.wb-sender__input');

            var modules = ['wx_emotion', 'hyperlink', 'image', 'voice', 'music', 'articles'
                        , 'goods', 'feature', 'homepage', 'usercenter'];

            // TODO 暂时仅在自动回复出现music模块
            if (['/v2/weixin/autoreply/normal', '/v2/weixin/autoreply/default'].indexOf(window.location.pathname) < 0) {
                // 不是服务号或者不是老的订阅号不出现音乐模块
                if (!_global.team_status.weixin_server || !_global.team_status.weixin_oldsub) {
                    modules = _.without(modules, 'music');
                }
            }

            if(minus){
                modules = modules.filter(function(item){
                    return !minus.some(function(it){
                        return it == item;
                    });
                });
            }

            view.editor = new Editor({
                config: {
                    data: data,
                    charLimit: 300,
                    picLimit: 1,
                    modules: modules,
                    thumb: ['goods', 'feature', 'homepage', 'usercenter']
                },
                el: view.editorEle
            });

            return view;
        },

        positioning: function() {
            var view = this;

            var className = view.options.className;
            view.el.className = className;

            view.$el.show();
            if (className.indexOf('left') !== -1) {
                view.$el.position({
                    of: view.target,
                    my: 'right center',
                    at: 'left center'
                });
            } else {
                view.$el.position({
                    of: view.target,
                    my: 'left center',
                    at: 'right center'
                });
            }
            view.editor.focus();
        },

        setData: function(data) {
            var view = this;

            view.editor.updateContent(data);
        },

        reset: function(obj) {
            var callback = obj.callback,
                target = obj.target,
                trigger = obj.trigger,
                data = obj.data,
                className = obj.className;

            this.options.className = className;
            this.setCallback(callback);
            this.setTarget(target);
            this.setTrigger(trigger);
            this.setData(data);
            this.positioning();
            this.show();
        },

        triggerCallback: function() {
            var view = this;

            var obj = view.editor.getContent();

            this.validateData(obj)
                .done(function(){
                    view.callback.call(view, obj);
                })
                .fail(function(reason){
                    utils.errorNotify(reason);
                })
                .always(function(){
                    view.hide();
                    view.editor.clear();
                });
        },

        validateData: function(data){
            var d = new $.Deferred();
            if(data.type == 'voice'){
                if($.getAudioDuration == undefined){
                    d.resolve();
                    console.error('没有引入getAudioDuration函数, 跳过验证');
                } else {
                    return $.getAudioDuration(data.attachment_url_cdn)
                        .then(function(duration){
                            if(Math.floor(duration) > 61){
                                return d.reject('音频不能超过60秒');
                            }
                        });
                }
            } else {
                d.resolve();
            }
            return d;
        }
    });
});
